<?php
/***
 * 
 * @author GuoLong
 * 2014/12/02
 * 
 */

class video extends Model {
	private $table="xdf_video";
	/**
	 * 添加video
	 */
	public function addVideo($user_id,$title,$name,$type,$time,$create_time) {
		$sql="INSERT INTO `$this->table` (`user_id`,`title`,`name`,`type`,`time`,`user_create_time`,`create_time`) 
		VALUES('$user_id','$title','$name','$type','$time','$create_time',NOW());";
		
		return $this->insert($sql);
	}
	
	/**
	 * 获取视频名称
	 */
	public function getVideoNameById($video_id){
		$sql = "SELECT name,user_id,title,time FROM `$this->table` WHERE id = $video_id";
		return $this->fetchRow( $sql );
	}
	public function viewVideo($video_id){
		$sql="UPDATE $this->table SET `view_num`=`view_num`+1 WHERE `id`='$video_id' ;";
		return $this->update($sql);
	}
	/*
	 * 根据视频id获取视频详情
	 * @author GuoLong
	* 2014/12/02
	*/
	//学生端获取视频详情
	public function getVideoInfoById($video_id) {
		$sql = "select 
					a.title as title,
					a.time as time,
					a.name as name,
					a.view_num as view_num,
					IFNULL(a.like_num,0) as like_num,
					IFNULL(a.dislike_num,0) as dislike_num,
					b.name as teacher,
					IFNULL(c.like_num,0) as mylike_num,
					IFNULL(c.dislike_num,0) as mydislike_num
				FROM `$this->table` a
				LEFT JOIN `xdf_user` b ON a.user_id=b.id
				LEFT JOIN `xdf_video_favor` c ON c.video_id=a.id  
				WHERE  a.id= '$video_id'; ";

		return $this->fetchRow( $sql );
	}
	//教师端获取视频详情
	public function teaGetVideoInfoById($video_id) {
		$sql = "select 
					u1.name as student,
					p.course as course,
					p.section as section,
					c.section_amount as section_amount,
					a.time as time,
					a.name as name,
					a.title as title,
					a.user_create_time as create_time,
					a.view_num as view_num,
					IFNULL(a.like_num,0) as like_num,
					IFNULL(a.dislike_num,0) as dislike_num
				FROM `$this->table` a
				LEFT JOIN `xdf_period` p ON p.video_id=a.id
				LEFT JOIN `xdf_class` c ON c.id=p.class_id
				LEFT JOIN `xdf_class_student` cs ON cs.class_id=p.class_id
				LEFT JOIN `xdf_user` u1 ON u1.id=cs.student_id
				WHERE  a.id= '$video_id'; ";
		return $this->fetchRow( $sql );
	}
	//获取某个人对某个视频的顶/踩总数
	public function getPersonFavorNum($user_id,$video_id){
		$sql="SELECT `like_num`,`dislike_num`  FROM `xdf_video_favor` WHERE `user_id`='$user_id' AND `video_id`='".$video_id."';";
		return $this->fetchRow($sql);
	}
	//对视频 顶踩
	public function likeVideo($user_id,$video_id,$like_num,$dislike_num){
		if($this->getPersonFavorNum($user_id, $video_id)){
			$sql="UPDATE xdf_video_favor SET `like_num`= `like_num`+'$like_num',`dislike_num`= `dislike_num`+'$dislike_num' WHERE `user_id`='$user_id' AND `video_id`='$video_id'";
			$updateVideoFavorNumSql="UPDATE $this->table SET like_num=like_num+$like_num,dislike_num=dislike_num+$dislike_num WHERE id='$video_id'";
			if($this->update($updateVideoFavorNumSql))
				return $this->update($sql);
			else 
				return false;
		}else{
			$sql="INSERT INTO `xdf_video_favor` (`user_id`,`video_id`,`like_num`,`dislike_num`)	VALUES($user_id,$video_id,$like_num,$dislike_num)";
			$updateVideoFavorNumSql="UPDATE $this->table SET like_num=like_num+$like_num,dislike_num=dislike_num+$dislike_num WHERE id='$video_id'";
			if($this->update($updateVideoFavorNumSql))
				return $this->insert($sql);
			else 
				return false;
		}
	}

	//教师端获取自己对题目历史视频
	public function teaGetVideoIdByItem($user_id,$item_id){
		$sql="SELECT iv.video_id,v.title,v.time,v.create_time from `xdf_item_video` iv
		LEFT JOIN `xdf_video` v ON v.id=iv.video_id
		WHERE item_id=$item_id AND v.user_id=$user_id AND visible = 1
		ORDER BY `create_time` DESC LIMIT 100000;";
		return $this->fetchAll($sql);
	}
	//学生用户user获取题目item的相关视频，只能查看自己老师的视频
	public function stuGetVideoByItem($user_id,$item_id){

		$sql = "SELECT iv.video_id 
				FROM `xdf_item_video` iv
				LEFT JOIN `xdf_video` v ON v.id=iv.video_id
				WHERE iv.item_id = '$item_id'
				AND v.user_id IN (
					SELECT teacher_id FROM `xdf_period`
					WHERE class_id IN (
						SELECT cs.class_id FROM `xdf_class_student` cs WHERE cs.`student_id`='$user_id'
					)
				)
				ORDER BY iv.id DESC LIMIT 1";
		return $this->fetchRow($sql);
	}
	
	
	//获取一道某个老师布置题目的视频id limit -1为学生取老师的最新题目视频
	public function getVideoByItem($user_id,$item_id,$limit){
		$sql = 'SELECT V.id,V.create_time FROM `'.$this->table.'` AS V
				WHERE V.id IN 
				(SELECT I.video_id FROM `xdf_item_video` AS I WHERE I.item_id = '.$item_id.')
				AND V.user_id = '.$user_id.' ORDER BY V.create_time DESC';
		if($limit ==1){
			$sql .=' LIMIT 1';
			return $this->fetchRow($sql);
		}else {
			return $this->fetchAll($sql);
		}
	}
	public function getVideoById($video_id){
		$sql="SELECT * FROM $this->table WHERE `id` = '$video_id';";
		return $this->fetchRow($sql);
	}
}
